%%%-------------------------------------------------------------------
%%% @author Rain
%%% @copyright (C) 2022, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 31. 3月 2022 17:40
%%%-------------------------------------------------------------------
-module(rank_test).
-author("Rain").
-include("rank.hrl").
-include("player.hrl").

%% API
-export([
    test_rank_submit/0,
    test_get/2]).



%% 随机一个从FromN-ToN的整数
rand_int(FromN, FromN) ->
    FromN;
rand_int(FromN, ToN) when ToN > FromN ->
    Lenth = ToN - FromN + 1,
    rand:uniform(Lenth) + FromN - 1;
rand_int(ToN, FromN) ->
    rand_int(FromN, ToN).


test_rank_submit() ->
    [begin
         Uuid = 10000 + Acc,
         Sex = rand_int(1, 0),
         Level = rand_int(1, 1000),
         Power = rand_int(1000, 99999),
         Icon = rand_int(1, 10),
         NameCntLeft = rand_int(3, 7),
         NameCntRight = rand_int(3, 7),
         NameLeft = [$a + rand_int(0, 25)||_<-lists:seq(1, NameCntLeft)],
         NameRight = [$a + rand_int(0, 25)||_<-lists:seq(1, NameCntRight)],
         Name = list_to_binary(NameLeft ++ " " ++ NameRight),
         UserData = #player{uuid = Uuid, sex = Sex, level = Level, power = Power, icon = Icon, name = Name},
         rank:submit(?RANK_TYPE_LEVEL, UserData),
         rank:submit(?RANK_TYPE_POWER, UserData)
         end||Acc<-lists:seq(1, 100)].

test_get(RankType, TopN) ->
    Ranks = rank:get_rank_data(RankType, TopN),
    Fun =
            fun(#rank_data{rank = Rank, briefs = Briefs, value = Value}) ->
                io:format("Rank ~p Name ~p Score ~p ~n",[Rank, maps:get(?RANK_BRIEF_KEY_NAME, Briefs, <<"">>), Value])
            end,
    lists:foreach(Fun, Ranks),
    ok.
